It is known to provide a data processing apparatus that is configured to execute a plurality of threads. Each thread will typically require a series of processing operations to be performed, and each thread will typically comprise a sequence of instructions defining those processing operations.
It is also known to utilise a lock as a synchronisation mechanism for limiting access to an associated resource (for example a shared data structure) within the data processing system when there are multiple threads of execution. If a processing operation requires access to a resource that is protected by a lock, then that processing operation will be referred to herein as a lock-protected processing operation, and prior to performing such a lock-protected processing operation for a particular thread, it will need to be ensured that the lock is acquired. Locks can take a variety of forms (and the present invention can be applied irrespective of the underlying form of lock), but one commonly used form is a binary value stored at a particular address in memory. To acquire the lock, the processing circuitry looks up the value at the lock address in memory, and if it is at a first value indicating that the lock is available (for example zero), then the lock is acquired by setting the data value to a second value to indicate that the lock has been acquired (for example one). Whilst the lock is acquired for a particular thread, any other thread seeking to acquire the look will determine on reading from the lock address that the lock is currently at the second value, and accordingly is already acquired. The lock-protected processing operation for that other thread can then not be performed until the thread has been released, and then subsequently acquired by that other waiting thread.
Accordingly, when multiple threads include a lock-protected processing operation that is protected by the same lock, then the lock-protected processing operation will only be able to be executed for one thread at a time. The lock-protected processing operation can be an identical operation to be performed by each thread, for example where each of the threads is executing the same program code, or may be a different processing operation in different threads, for example where one thread wishes to perform a processing operation to access a shared resource in order to add values to a list in that shared resource, whilst another thread wishes to perform a processing operation to delete values from the list in that shared resource.
The one or more instructions specifying a lock-protected processing operation will be referred to herein as a critical code section, and accordingly a critical code section is a part of a multi-threaded program which can only be safely executed by one thread at a time, for example because it updates a shared data structure. When encountering a critical code section within a particular thread, the lock is first acquired (which prevents any other thread acquiring the lock until it is released), and after completing the critical code section, the lock is released. Such an approach allows critical code sections of arbitrary complexity to be supported by any hardware which can support a lock mechanism.
However, to ensure correct operation, the lock mechanism must involve communication and synchronisation between threads, such that all threads understand which thread has the lock at any one time, and furthermore this mechanism must be invoked each time the lock is acquired and released (i.e. each time each thread wants to execute the lock-protected processing operation). Typically this is achieved using atomic operations which ensure that only a single thread can acquire the lock at a time. The acquisition of a lock is hence a relatively time consuming process, involving the execution of multiple instructions, and read and write accesses to the lock address in memory. It would accordingly be desirable to provide a more efficient mechanism for handling lock-protected processing operations when executing multiple threads on a data processing apparatus.